//
//  SQNetWorking.m
//  SQ_AProject
//
//  Created by 宋强 on 16/3/7.
//  Copyright © 2016年 宋强. All rights reserved.
//

#import "SQNetWorking.h"

@implementation SQNetWorking
+ (void)GETNetWithUrl:(NSString *)url body:(id)body headerFile:(NSDictionary *)header response:(SQResponseStyle)responseStyle success:(BlockOfSuccess)success failure:(void (^)(NSError *))failure
{

   // hud.color = [UIColor clearColor];
    //    1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    2.设置请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    //    3.设置返回数据的类型
    switch (responseStyle) {
        case SQJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            
            break;
        case SQXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            
            break;
        case SQDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            
            break;
        default:
            break;
    }
    //    4.设置响应数据的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    //    5.UTF－8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //    6.请求数据
    [manager GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
            success(responseObject);
                   }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
            
        }
    }];
    
}
+ (void)POSTNetWithUrl:(NSString *)url body:(id)body bodyStyle:(SQRequestStyle)bodyRequestStyle headerFile:(NSDictionary *)header response:(SQResponseStyle)responseStyle success:(BlockOfSuccess)success failure:(void (^)(NSError *))failure
{
    //    1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //    2.设置Body体数据类型
    switch (bodyRequestStyle) {
        case SQRequestJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            
            break;
        case SQRequestString:
            [ manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return parameters;
            }];
            break;
            
        default:
            break;
    }
    
    
    
    
    //    2.设置请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    //    3.设置返回数据的类型
    switch (responseStyle) {
        case SQJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            
            break;
        case SQXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            
            break;
        case SQDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            
            break;
        default:
            break;
    }
    //    4.设置响应数据的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    //    5.UTF－8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //    6.请求数据
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject) {
            success(responseObject);
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
            failure(error);
        }
        
    }];
    

}
@end
